/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Yi Dev, Ltd. All Right Reserved.
 * @address:     https://yiyuery.github.io/NoteBooks/
 * @date:        2019/12/13 12:05 上午
 * @email:       xiazhaoyang@live.com
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.example.spring.aop.advice.introduce;

import com.example.spring.aop.monitor.BusinessLogMonitor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.support.DelegatingIntroductionInterceptor;

import java.util.Objects;

/**
 * <p>
 *
 * </p>
 *
 * @author Yi
 * @version V1.0.0
 * @date 2019/12/13 12:05 上午
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人}  2019/12/13
 * @modify reason: {方法名}:{原因}
 * ...
 */
public class BusinessLogHandlerIntroduceAdvice extends DelegatingIntroductionInterceptor implements BusinessLogHandlerIntroduceSwitch {

    private ThreadLocal<Boolean> switchMap = new ThreadLocal<>();

    @Override
    public void setSwitch(boolean open) {
        switchMap.set(open);
    }

    @Override
    public Object invoke(MethodInvocation mi) throws Throwable {
        Object obj = null;
        if (Objects.nonNull(switchMap.get()) && switchMap.get()) {
            BusinessLogMonitor.begin(mi.getMethod().getName());
            obj = super.invoke(mi);
            BusinessLogMonitor.end();
        }else{
            obj = super.invoke(mi);
        }
        return obj;
    }
}




